package com.bigzero.workflow.common.config;

import com.bigzero.workflow.common.dao.TableDao;
import com.bigzero.workflow.common.dao.TableMySQLDao;
import com.bigzero.workflow.common.dao.TablePostgreSQLDao;
import com.bigzero.workflow.common.enums.DbType;
import com.bigzero.workflow.common.utils.PropertiesUtil;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.Map;

/**
 * 数据库配置
 *
 * @author fanxinxiong
 * @date 2025-02-08 11:09
 **/
@Configuration
public class DbConfig {

    @Value("${system.database}")
    private DbType database;

    @Getter
    private Map<String, String[]> widgetDataType;
    @Getter
    private Map<String, Map.Entry<String, Integer>> defaultWidgetDataType;


    // 数据库操作实现
    @Autowired
    private TableMySQLDao tableMySQLDao;
    @Autowired
    private TablePostgreSQLDao tablePostgreSQLDao;

    /**
     * 切换数据库
     */
    @Bean
    @Primary
    public TableDao getTableDao() {
        switch (database) {
            case MY_SQL:
                widgetDataType = PropertiesUtil.getWidgetDataType(DbType.MY_SQL.getKey());
                defaultWidgetDataType = PropertiesUtil.getDefaultWidgetDataType(DbType.MY_SQL.getKey());
                return tableMySQLDao;
            case POSTGRE_SQL:
                widgetDataType = PropertiesUtil.getWidgetDataType(DbType.POSTGRE_SQL.getKey());
                defaultWidgetDataType = PropertiesUtil.getDefaultWidgetDataType(DbType.POSTGRE_SQL.getKey());
                return tablePostgreSQLDao;
            default:
                throw new RuntimeException("不支持当前数据库：" + database);
        }
    }

}
